package com.zhc.dejingapi.job;

import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import com.zhc.dejingapi.pojo.dingtalk.DingTalkApiInfo;
import com.zhc.dejingapi.service.dingtalk.IProcessDingCallBackDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import shade.com.alibaba.fastjson2.JSONObject;


@Slf4j
@Configuration
public class DingClallBackJob {
    private static Boolean isConnect=false;

    @Autowired
    private DingTalkApiInfo dingTalkApiInfo;
    private final IProcessDingCallBackDataService processDingCallBackDataService;

    public DingClallBackJob(IProcessDingCallBackDataService processDingCallBackDataService1) {
        this.processDingCallBackDataService = processDingCallBackDataService1;
    }

    public static void main(String[] args) {

        String clientId="dingxvjxlbpwej6gldx1";
        String clientSecret="FlY5tW39ADNBWc8Cw4o8aLh2iVcFqKbrBFJL7ulhNukd2XwLPZ-ERVle952RHLi0";

        clientId="dingkv33jljphnfqf1xh";
        clientSecret="ZzEQmPBy2ba7bTFqeOF4wH28my--EPLb8HkH80OQV7cfxP68t9Ac_KSag1pW4lz4";

        try{
            OpenDingTalkStreamClientBuilder
                    .custom()
                    .credential(new AuthClientCredential(clientId, clientSecret))
                    //注册事件监听
                    .registerAllEventListener(new GenericEventListener() {
                        public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                            try {
                                //事件唯一Id
                                String eventId = event.getEventId();
                                //事件类型
                                String eventType = event.getEventType();
                                //事件产生时间
                                Long bornTime = event.getEventBornTime();
                                //获取事件体
                                JSONObject bizData = event.getData();
                                //处理事件
                                //process(bizData);
                                log.info(bizData.toString());
                                //消费成功
                                return EventAckStatus.SUCCESS;
                            } catch (Exception e) {
                                //消费失败
                                return EventAckStatus.LATER;
                            }
                        }
                    })
                    .build().start();
        }
        catch (Exception e){
            e.printStackTrace();
            log.error("DingClallBackJob.executeJob() Exception:"+e);

        }

    }

    public void executeJob() {

        if (isConnect){
            return;
        }
        String clientId= dingTalkApiInfo.getAppKey();
        String clientSecret= dingTalkApiInfo.getAppSecret();


        int i=1;
        if(i==10){
            try {

                OpenDingTalkStreamClientBuilder
                        .custom()
                        .credential(new AuthClientCredential(clientId, clientSecret))
                        //注册事件监听
                        .registerAllEventListener(new GenericEventListener() {
                            public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                                try {
                                    //事件唯一Id
                                    String eventId = event.getEventId();
                                    //事件类型
                                    String eventType = event.getEventType();
                                    //事件产生时间
                                    Long bornTime = event.getEventBornTime();
                                    //获取事件体
                                    JSONObject bizData = event.getData();
                                    //处理事件
                                    //process(bizData);
                                    Boolean bDealOK = processDingCallBackDataService.ProcessData(eventId, eventType, bornTime, bizData);
                                    //消费成功
                                    if(bDealOK){
                                        return EventAckStatus.SUCCESS;
                                    }
                                    else{
                                        return EventAckStatus.LATER;
                                    }

                                    //return EventAckStatus.SUCCESS;
                                } catch (Exception e) {
                                    //消费失败
                                    return EventAckStatus.LATER;
                                }
                            }
                        })
                        .build().start();
                isConnect=true;
            }
            catch (Exception e){
                e.printStackTrace();
                log.error("DingClallBackJob.executeJob() Exception:"+e);
                isConnect=false;
            }
        }


    }

}
